import wx
import os.path
import cStringIO
import __main__

from utils import tracer

IMG_PROJECT = 'project'
IMG_PACKAGE = 'package'
IMG_MODULE = 'module'
IMG_CLASS = 'class'
IMG_METHOD = 'method'
IMG_IMPORT = 'import'

IMG_CONFIG_FILE = 'cfg'
IMG_INI_FILE = 'ini'
IMG_XML_FILE = 'xml'
IMG_TEXT_FILE = 'txt'
IMG_IMAGE_FILE = 'image'

def getImage(itemname):
  imagesPath = os.path.normpath(__main__.app.globalSettings().getHomePath()) + os.path.sep + 'images'
  imageFile = imagesPath + os.path.sep + itemname.lower() + os.path.extsep + 'png'

  if not os.path.exists(imageFile):
    return None

  tracer.writeln('Load image from file %s' % imageFile)

  data = open(imageFile, "rb").read()
  stream = cStringIO.StringIO(data)

  image = wx.ImageFromStream(stream)
  if image is None:
    tracer.writeln('Load image failed.')
    return None

  tracer.writeln('Image size: %dX%d' % (image.GetWidth(), image.GetHeight()))
  if image.GetWidth() != 16 or image.GetHeight() != 16:
    image.Rescale(16, 16)

  bmp = wx.BitmapFromImage(image)
  if bmp is None:
    tracer.writeln('Convert image to bitmap failed.')

  return bmp
